/**
 * Created by wangshaohua on 2018/1/13.
 */
moduleLearningMap.controller("LearningMapCourseDetail", function (LanguageService, LearningMapExerciseResource, $$CambioConfig, UploadFileService, $scope, $log, $state, $stateParams, GeneralResource, LoadingService, ConstantService, DialogService, LearningMapCourseResource) {
    initPage($scope);
    $scope.$on("$viewContentLoaded", function () {
        //$scope.reset();
        pageOnLoad();
        ConstantService.addState($state.current, true, LanguageService.language('学习地图章节详细'));
        $scope.learningMapCourseId = $stateParams.learningMapCourseId;
        $scope.learningMapId = $stateParams.learningMapId;
        $scope.selectTab('tabInfo');
    });
    
    $scope.returnMap = function () {
        $state.go("sz.learningMapDetail", {
            learningMapId: $scope.learningMapId
        });
    }

    $scope.currentTabId = 0;
    $scope.selectTab = function (tabId) {
        if (tabId === 'tabInfo') {
            $scope.loadDetail();
            $scope.currentTabId = tabId;
        }
        if (tabId === 'tabExercise' && $scope.learningMapCourseId) {
            $scope.loadExercise();
            $scope.currentTabId = tabId;
        }
    };

    // ------------------------------------------------------------------------------------------------------

    $scope.queryPara = {
        className: Models.LearningMapCourse.className
    };

    $scope.loadDetail = function () {
        if ($scope.learningMapCourseId) {
            //LoadingService.show();
            GeneralResource.findById({id: $scope.learningMapCourseId}, $scope.queryPara, function (queryResult) {
                $log.debug("查询结果", queryResult.data);
                $scope.learningMapCourseDetail = queryResult.data;
                //LoadingService.hide();
            });
        } else {
            $scope.learningMapCourseDetail = {
                learningMapId: $scope.learningMapId,
                picture: LearningMapTemplateUtils.findCourse().picture
            };
        }
    };

    $scope.saveCourse = function () {
        Alert.confirm(function () {
            //LoadingService.show();
            if ($scope.learningMapCourseId) {
                LearningMapCourseResource.update({}, $scope.learningMapCourseDetail, function (queryResult) {
                    $log.debug("查询结果", queryResult.data);
                    //$scope.learningMapCourseDetail = queryResult.data;
                    Alert.success();
                    $scope.loadDetail();
                    //LoadingService.hide();
                });
            }
        })
    }

    // ------------------------------------------------------------------------------------------------------
    // ------------------------------------------------------------------------------------------------------

    $scope.loadExercise = function () {
        //LoadingService.show();
        LearningMapExerciseResource.findByCourseId({learningMapCourseId: $stateParams.learningMapCourseId}, {}, function (queryResult) {
            $log.debug("查询结果", queryResult.data);
            $scope.exercises = queryResult.data;
            //$scope.pagination.setPage(queryResult.data, pageNo);
            //LoadingService.hide();
        });
    }

    $scope.configExercise = function () {
        if (!$scope.exercises) {
            $scope.exercises = new Array();
        }
        $scope.currentContent = {
            sort: $scope.exercises.length + 1
        };
        DialogService.open("exerciseContentSelect", $scope, function () {
            LearningMapExerciseResource.quickConfiyContentIds({learningMapCourseId: $scope.learningMapCourseId}, $scope.selectContentIds, function () {
                $scope.selectTab('tabExercise');
            });
            // $scope.loadExercise();
        }, '80%');
    }

    $scope.upExerciseSort = function (record) {
        record.sort++;
        record.needSave = true;
    }

    $scope.downExerciseSort = function (record) {
        if (record.sort > 0) {
            record.sort--;
        }
        record.needSave = true;
    }

    $scope.saveCurrentExercise = function (record) {
        var bean = {
            learningMapExerciseId: record.id,
            sort: record.sort
        }
        Alert.confirm(function () {
            LearningMapExerciseResource.updateSort({}, bean, function () {
                Alert.success();
                record.needSave = false;
                $scope.$apply();
            });
        });
    }

    $scope.removeExercise = function (exercise) {
        Alert.confirm(function () {
            LearningMapExerciseResource.remove({id: exercise.id}, {}, function () {
                Alert.success();
                $scope.loadExercise();
            });
        })
    }

    // ------------------------------------------------------------------------------------------------------
    // ------------------------------------------------------------------------------------------------------

    $scope.uploadCourseImage = function ($files) {
        if ($files && $files[0]) {
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'IMG', $scope).then(function (o) {
                $scope.learningMapCourseDetail.picture = o.id;
            });
        }
    };
});